from __future__ import with_statement
from fabric.api import *
from datetime import *

# This is an sample fabric deployment script for automated deployment of a
# Django app. A lot of it depends on your target environment, but it should be
# fairly straightforward to adapt to your own needs.

def production():
    """Information about the production environment."""
	env.hosts = ['example.com'] # The remote server address
	env.user = '<username>' # SSH username
	env.now = datetime.utcnow().strftime("%Y%m%d-%H%M%S")
	env.release_dir = "myapp-%s" % env.now # Used for naming files in the release

def syncdb():
	"""Run the syncdb comand on the remote server"""
	run("cd /remote/path/myapp && python manage.py syncdb")

def build():
    """Gather files for deployment in a local zipfile. (make sure the
    tmp-release dir exists."""
	local("zip -r ../tmp-release/%s.zip ./* -x *.svn* .DS_Store *.pyc fabfile.py settings.py cronjobs.*" % env.release_dir)	

def upload():
	"""Put the release zip on the remote server."""
	put("../tmp-release/%s.zip" % (env.release_dir), "/remote/path/myapp-deploy/%s.zip" % env.release_dir)

def restart():
    """Restart the remote server (restarts lighty)."""
	run("/remote/path/init/myapp restart")

def deploy_files():
	"""Stop server and deploy new files."""
	run("/remote/path/init/myapp stop")

    # Unzip released files to a timestamped folder
	run("unzip -oq ~/myapp-deploy/%s.zip -d ~/releases/%s/" % (env.release_dir, env.release_dir))

    # See rollback method
	#run("mv /remote/path/myapp /remote/path/myapp-previous")
	
    # Symlink webserver dirs to release dirs and other fodlers
    run("ln -sf /remote/path/releases/%s /remote/path/myapp" % env.release_dir)
	run("ln -sf /remote/path/settings/settings.py /remote/path/myapp/settings.py")
	run("ln -sf /remote/path/settings/settings-deployment.py /remote/path/myapp/settings-deployment.py")
	run("ln -sf /remote/path/settings/myapp.pid /remote/path/myapp/myapp.pid") #for lighty

    # Upload local settings to make it easier to see difference from prod settings
	put("settings.py", "/remote/path/myapp/dev_settings.py")

    # Start server
	run("/remote/path/init/myapp start")

    # List remote dir to see that all symlinks look ok
	run("ls -l")

def clear_cache():
	"""Clear the django cache folder"""
	run("rm -rf /remote/path/to/django-cache/*")

def deploy():
	"""Build the project and deploy it to a specified environment."""
	build()
	upload()
	deploy_files()
	clear_cache()

# Not implemented yet. Idea is to symplink the previous release to
# "myapp-previous" to enable a quick rollback.
def rollback():
	"""Rollback to the previous release"""
    pass
	#run("ls -l")
	#run("/remote/path/init/myapp stop")
	#run("rm /remote/path/myapp")
	#run("mv /remote/path/myapp-previous /remote/path/myapp")
	#clear_cache()
	#run("/remote/path/init/myapp start")
	#run("ls -l")

# Not implemented yet. Idea is to dump remote mysql data to a file, transfer it
# to the local computer and create a new timestamped database
# "opengov_prod_yymmdd_hhmmss" for easy debugging related to data in the
# production system. Or for a simple backup.
def get_data():
    pass

